package capaDatos;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import utiles.UIDGenerator;

import capaLogica.Comentario;
import capaLogica.Evento;
import capaLogica.Noticia;

public class ComentarioDAO {

	/**
	 * Atributo que crea la conexion a la bd
	 */
	private Connection conn;
	
	/**
	 * Constructor de la clase
	 */
	public ComentarioDAO(){
		conn = (Connection) ConnectionManager.getInstance().checkOut();
	}
	
	@Override
	 protected void finalize() {
       ConnectionManager.getInstance().checkIn(conn);
	}
	 
	public Comentario recuperarComentarioById(String evento) {
        PreparedStatement stmt = null;
        ResultSet result = null;
        Comentario p = null;
        String sql = "SELECT * FROM comentario WHERE (idComentario = ?) ";

        try {
            stmt = conn.prepareStatement(sql);
            stmt.setString(1, evento);
            result = stmt.executeQuery();

            if ( result.next() ){
	            p = new Comentario();
	            p.setIdComentario(result.getString("idComentario"));
	            p.setIdUsuarioCreador(result.getString("idUsuarioCreador"));
	            p.setDescripcion(result.getString("descripcion"));
	            p.setFechaPublicacion(result.getDate("fechaPublicacion"));
            }
            
        } catch (SQLException e) {
            System.out.println("Message: " + e.getMessage());
            System.out.println("SQLState: " + e.getSQLState());
            System.out.println("ErrorCode: " + e.getErrorCode());
        } finally {
            try {
                if (result != null) {
                    result.close();
                }
                if (stmt != null) {
                    stmt.close();
                }
            } catch (SQLException e) {
            }
        }
        return p;
    }
	
	public List<Comentario> recuperarTodos() {
        PreparedStatement stmt = null;
        ResultSet result = null;
        List<Comentario> listaC = new ArrayList<Comentario>();
        String sql = "SELECT * FROM comentario";

        try {
            stmt = conn.prepareStatement(sql);
            result = stmt.executeQuery();

            while ( result.next() ){
	            Comentario p = new Comentario();
	            p.setIdComentario(result.getString("idComentario"));
	            p.setIdUsuarioCreador(result.getString("idUsuarioCreador"));
	            p.setDescripcion(result.getString("descripcion"));
	            p.setFechaPublicacion(result.getDate("fechaPublicacion"));
	            listaC.add(p);
            }
            
        } catch (SQLException e) {
            System.out.println("Message: " + e.getMessage());
            System.out.println("SQLState: " + e.getSQLState());
            System.out.println("ErrorCode: " + e.getErrorCode());
        } finally {
            try {
                if (result != null) {
                    result.close();
                }
                if (stmt != null) {
                    stmt.close();
                }
            } catch (SQLException e) {
            }
        }
        return listaC;
    }
	
	 public Comentario insertarComentario(Comentario n) {
			String sql = "INSERT INTO comentario (idComentario,idUsuarioCreador,descripcion,fechaPublicacion) VALUES (?,?,?,?) ";
			PreparedStatement stmt = null;
			
			try {
				stmt = conn.prepareStatement(sql);
				n.setIdComentario(UIDGenerator.getInstance().getKey());
				
				java.util.Date fechaNoticia= n.getFechaPublicacion();
	            
				stmt.setString(1, n.getIdComentario());
				stmt.setString(2, n.getIdUsuarioCreador());
				stmt.setString(3, n.getDescripcion());
				stmt.setDate(4, new java.sql.Date(fechaNoticia.getTime()));
				stmt.executeUpdate();

			} catch (SQLException e) {
				System.out.println("Message: " + e.getMessage());
				System.out.println("SQLState: " + e.getSQLState());
				System.out.println("ErrorCode: " + e.getErrorCode());
			} finally {
				try {
					if (stmt != null) {
						stmt.close();
						
					}
				} catch (SQLException e) {
				}
			}
			
			return n;
		}
	 
	 public void borrarNoticia(String idNoticia) {
			PreparedStatement stm = null;
			String sql = "DELETE  FROM comentario WHERE (idComentario = ?)";
			try {
				stm = conn.prepareStatement(sql);
				stm.setString(1, idNoticia);
				stm.executeUpdate();
				
			} catch (SQLException e) {
				System.out.println("SQLMessage: " + e.getMessage());
				System.out.println("SQLState: " + e.getSQLState());
				System.out.println("ErrorCode: " + e.getErrorCode());
			} finally {
				try {
					if (stm != null) {
						stm.close();
					}
				} catch (SQLException e) {

				}
			}
		}	
	 
	 public void insertarAsociativa(String idEvento, String idComentario) {
			String sql = "INSERT INTO comentariosEvento (idComentarioEvento,idEvento,idComentario) VALUES (?,?,?) ";
			PreparedStatement stmt = null;
			
			try {
				stmt = conn.prepareStatement(sql);
			
	            
				stmt.setString(1, UIDGenerator.getInstance().getKey());
				stmt.setString(2, idEvento);
				stmt.setString(3, idComentario);
				stmt.executeUpdate();

			} catch (SQLException e) {
				System.out.println("Message: " + e.getMessage());
				System.out.println("SQLState: " + e.getSQLState());
				System.out.println("ErrorCode: " + e.getErrorCode());
			} finally {
				try {
					if (stmt != null) {
						stmt.close();
						
					}
				} catch (SQLException e) {
				}
			}
		}
}
